<?php
 defined('_JEXEC') OR defined('_VALID_MOS') OR die('...Direct Access to this location is not allowed...');
### @copyright Copyright (C) 2006-2010 Joobi Limited All rights reserved.

jimport('joomla.plugin.plugin');


/**
 * Plugin to sync user with Acajoom
 */
class plgUserAcasyncuser extends JPlugin {

	function plgUserAcasyncuser( &$subject, $config) {
		parent::__construct($subject, $config);
	}//endfct


	/**
	 * Example store user method
	 *
	 * Method is called after user data is stored in the database
	 *
	 * @param 	array		holds the new user data
	 * @param 	boolean		true if a new user is stored
	 * @param	boolean		true if user was succesfully stored in the database
	 * @param	string		message
	 */
	function onAfterStoreUser($user, $isnew, $success, $msg)
	{
		global $mainframe;

		if ( strtolower( substr( JPATH_ROOT, strlen(JPATH_ROOT)-13 ) ) =='administrator' ) {	// joomla 15
			$adminPath = strtolower( substr( JPATH_ROOT, strlen(JPATH_ROOT)-13 ) );
		} else {
			$adminPath = JPATH_ROOT;
		}//endif

		if ( !@include_once( $adminPath . '/components/com_acajoom/defines.php') ) return;
		include_once( WPATH_CLASS . 'class.acajoom.php');

		// convert the user parameters passed to the event
		// to a format the external application

		if ($isnew) {

			$subscriber = null;
			$subscriberId = 0;
			$subscriber->user_id = $user['id'];
			$subscriber->name = $user['name'];
			$subscriber->email = $user['email'];
			$subscriber->receive_html = 1;
			$subscriber->confirmed = 1;
            $subscriber->subscribe_date = date("Y-m-d H:m:s");
            $subscriber->language_iso = 'eng';
            $subscriber->timezone = '00:00:00';
            $subscriber->blacklist = 0;
            $subscriber->params = '';

			//notice columns
			if($GLOBALS[ACA.'type']=='PRO'){//check if the version of acajoom is pro
			$subscriber->column1='';
			$subscriber->column2='';
			$subscriber->column3='';
			$subscriber->column4='';
			$subscriber->column5='';
			}//end if check if the version is pro

            subscribers::insertSubscriber($subscriber, $subscriberId);
			//subscribers::syncSubscribers($isnew);

            //added by mary
			$database =& JFactory::getDBO();

			//get subscriber id of the new user
			$sId= subscribers::getSubscriberId($subscriber->subscribe_date);

			//$query='Select `id` from `#__acajoom_subscribers` where `user_id`='.$subscriber->user_id;
			//$database->setQuery($query);
			//$sId=$database->loadObjectList();

			//get list ids of auto_add lists
			$query='Select id from `#__acajoom_lists` where `auto_add`=1';
			$database->setQuery($query);
			$autoListId=$database->loadObjectList();
			$error = $database->getErrorMsg();
			if (!empty($error)){
				echo  $error;
				return false;
			}else{
				foreach($autoListId AS $autoId){
					$queue = new stdClass;
				    $queue->id = 0;
			        $queue->subscriber_id = $sId;
			        $queue->list_id = $autoId->id;
			        $queue->type = 1;
			        $queue->mailing_id = 0;
			        $queue->send_date = '0000-00-00 00:00:00';
			        $queue->suspend = 0;
			        $queue->delay = 0;
//			        if ( ACA_CMSTYPE ) {	// joomla 15
//						$queue->acc_level = intval(JRequest::getVar('acc_level', 29));
//					} else {									//joomla 1x
//						$queue->acc_level = intval(mosGetParam($_REQUEST, 'acc_level', 29));
//					}//endif
			        $queue->acc_level = intval(JRequest::getVar('acc_level', 29));
			        $queue->issue_nb = 0;
			        $queue->published = 0;
			        $queue->params = '';
			        queue::insert($queue);
				}//end of foreach
			}//end if
			//mary
		}//endif

	}//endif


}//endclass

